<?php

namespace App\Http\Controllers;

use App\CodeResponse;
use App\Events\WithdrawalEvent;
use App\Services\Calculation\CalculationServices;
use App\Services\cart\CartRedisHashServices;
use App\Services\Goods\GoodsServices;
use App\Services\RabbitMQ\LXServices;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redis;
use function DI\string;

class GoodsController extends BaseController
{
    /**
     * 模拟设计商品sku
     * @return \Illuminate\Http\JsonResponse
     * @throws \App\Exceptions\BusinessException
     */
    public function particulars()
    {
        Redis::incr('liangxuan');//执行就+1 redis实现PV 访问量
        $goodsId = $this->verifyInteger('goods_id');
        $goodsArr = GoodsServices::getInstance()->goodsDetails($goodsId);
        if (!$goodsArr) {
            return $this->fail(CodeResponse::FAIL, '参数错误');
        }
        return $this->success($goodsArr);
    }

    /**
     * 模拟添加购物车
     * @return \Illuminate\Http\JsonResponse
     * @throws \App\Exceptions\BusinessException
     */
    public function saveCart()
    {

        $goodsId = $this->verifyInteger('goods_id');
        $num = $this->verifyInteger('num');
        $userId = 123;
        $ret = CartRedisHashServices::getInstance()->saveCart($goodsId, $userId, $num);
        if (is_string($ret)) {
            return $this->fail(CodeResponse::FAIL, $ret);
        }
        return $this->success();
    }

    /**
     * 模拟获取购物车列表
     * @throws \App\Exceptions\BusinessException
     */
    public function cartList(Request $request)
    {
        $goodsId = $request->input('goods_id');
        $type = $request->input("type", "all");
        $list = CartRedisHashServices::getInstance()->getCartListAllOrById(123, $goodsId, $type);
        dd($list);
    }

    /**
     * 模拟删除或清空购物车
     * @return \Illuminate\Http\JsonResponse
     */
    public function delCart()
    {
        $type = "id";
        $goodsId = 1;
        $ret = CartRedisHashServices::getInstance()->delCartAllOrById(123, $goodsId, $type);
        if (!$ret) {
            return $this->fail();
        }
        return $this->success();
    }

    /**
     * 模拟修改购物车 redisHash
     * @return \Illuminate\Http\JsonResponse
     * @throws \App\Exceptions\BusinessException
     */
    public function updCart()
    {
        $goodsId = $this->verifyInteger('goods_id');
        $userId = 123;
        $sku = 'XXL/红色';
        $num = 56;
        $res = CartRedisHashServices::getInstance()->updCart($userId, $goodsId, $num, $sku);
        if (!$res) {
            return $this->fail();
        }
        return $this->success();
    }

    public function date()
    {
        $data = GoodsServices::getInstance()->goodsM();
        dd($data);
    }

//    public function twoSum($nums = [3,2,4], $target = 6) {
//        $data = [];
//        for($i=0;$i<count($nums);$i++){
//            if(empty($data)){
//                for($j=0;$j<count($nums);$j++){
//                    if($i != $j) {
//                        if (($nums[$i] + $nums[$j]) == $target) {
//                            $data = [$j, $i];
//                        }
//                    }
//                }
//            }
//        }
//        echo '<pre>';
//        print_r($data);
//        echo '</pre>';
//        die();
//    }

    public function twoSum(Request $request)
    {
//        $add = 0;
//        while (isset($a->val) || isset($b->val) || $add > 0) {
//            $valueA = 0;
//            if (isset($a->val)) {
//                $valueA = $a->val;
//                $a = $a->next;
//            }
//            $valueB = 0;
//            if (isset($b->val)) {
//                $valueB = $b->val;
//                $b = $b->next;
//            }
//            $current = isset($add) ? $add : 0;
//            $val = ($valueA + $valueB + $current) % 10;
//            $add = 0;
//            if ($valueA + $valueB + $current >= 10) {
//                $add = 1;
//            }
//            if (empty($node)) {
//                $node = new ListNode($val);
//                $next = $node;
//                continue;
//            }
//            $next = $next->next = new ListNode($val);
//        }
//
//        return $node;
        header("Content-Type:image/jpg");
        $secretId = "AKID2wd7OUM1RdaItHQL1SebJtMekdHj15ua"; //替换为用户的 secretId，请登录访问管理控制台进行查看和管理，https://console.cloud.tencent.com/cam/capi
        $secretKey = "XqujJekbDajdIkxve7MPDm8HFzUkkTw9"; //替换为用户的 secretKey，请登录访问管理控制台进行查看和管理，https://console.cloud.tencent.com/cam/capi
        $region = "ap-shanghai"; //替换为用户的 region，已创建桶归属的region可以在控制台查看，https://console.cloud.tencent.com/cos5/bucket
        $cosClient = new \Qcloud\Cos\Client(
            array(
                'region' => $region,
                'schema' => 'https', //协议头部，默认为http
                'credentials' => array(
                    'secretId' => $secretId,
                    'secretKey' => $secretKey
                )
            ));
//        try {
//            $bucket = "coolcar-1308815253"; //存储桶，格式：BucketName-APPID
//            $key = "longzhu.jpg";  //此处的 key 为对象键，对象键是对象在存储桶中的唯一标识
//            $result = $cosClient->getObject(array(
//                'Bucket' => $bucket,
//                'Key' => $key));
//            // 请求成功
//            echo $result['Body'];
//        } catch (\Exception $e) {
//            // 请求失败
//            echo "$e\n";
//        }
//        echo phpinfo();
        ### 上传文件流
        try {
            $bucket = "coolcar-1308815253"; //存储桶名称 格式：BucketName-APPID
            $key = "cee.jpg"; //此处的 key 为对象键，对象键是对象在存储桶中的唯一标识
//            $srcPath = "d:\Users\admin\Desktop\c5e609cf9ed4d0711f9c5affd716ca5d.png";//本地文件绝对路径
            $file = $request->file("file");
            if ($file) {
                $result = $cosClient->putObject(array(
                    'Bucket' => $bucket,
                    'Key' => $key,
                    'Body' => file_get_contents($file)
                ));
                print_r($result);
            }
        } catch (\Exception $e) {
            echo "$e\n";
        }
    }
}
